<?php
//活动专题
// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------


namespace Admin\Controller;
use Admin\Controller\AdminController;

class TopicController extends AdminController {
    
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'operate'); // 数据集
    }
    //put your code here
    public function index(){
        $navi = M('topic');
        $count = $navi->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $list = $navi->order('topic_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('pages', $show); // 赋值分页输出
        $this->assign('action', 'topic');
        $this->display();
    }
    
    
    public function add(){
        if (IS_POST) {
            $data['title'] = I('title');
            $data['intro'] = I('intro');
            $data['start_time'] = strtotime(I('start_time'));
            $data['end_time'] = strtotime(I('end_time'));
            $data['template'] = I('template');
            $data['css'] = I('css');
            $data['pc_img'] = I('pc_img');
            $data['mb_img'] = I('mb_img');
            $data['base_style'] = I('base_style');
            $data['keywords'] = I('keywords');
            $data['description'] = I('description');
            $data['created'] = NOW_TIME;
            
            $re = M('topic')->add($data);
            if ($re) {
                $this->success('活动添加成功，请上传活动产品！');
            } else {
                $this->error('活动添加失败！');
            }
        }
        $this->assign('action', 'topic');
        $this->display();
    }
    
    
    
    public function edit(){
        $topic_id = I('topic_id');
        $map['topic_id'] = $topic_id;
        if (IS_POST) {
            $data['title'] = I('title');
            $data['intro'] = I('intro');
            $data['start_time'] = strtotime(I('start_time'));
            $data['end_time'] = strtotime(I('end_time'));
            $data['template'] = I('template');
            $data['css'] = I('css');
            $data['pc_img'] = I('pc_img');
            $data['mb_img'] = I('mb_img');
            $data['base_style'] = I('base_style');
            $data['keywords'] = I('keywords');
            $data['description'] = I('description');
            $re = M('topic')->where($map)->save($data);
            if ($re) {
                $this->success('活动添加成功，请上传活动产品！');
            } else {
                $this->error('活动添加失败！');
            }
        }
        
        $topic = M('topic')->where($map)->find();
        $this->assign('topic',$topic);
        $this->assign('action', 'topic');
        $this->display();
    }
    
    public function addGoods(){
        $topic_id = I('get.topic_id');
        $map['topic_id'] = $topic_id;
        if(IS_POST){
            $data = serialize($_POST);
            $re = M('topic')->where($map)->setField('data',$data);
            if ($re) {
                $this->success('专题添加商品成功！');
            } else {
                $this->error('专题添加商品失败！');
            }
        }
        $data = M('topic')->where($map)->getField('data');
        $goodList = unserialize($data);
        foreach ($goodList as $key => $value) {
            foreach ($value as $k => $v) {
                $map['goods_id'] = $v;
                $goodList[$key][$k] = M('goods')->field('goods_id,goods_title,goods_buytitle,goods_thumb,goods_price,goods_markprice')->where($map)->find();
            }
        }

        $this->assign('goodList',$goodList);
        $cate = S('tdd_category');
        $this->assign('cate',$cate);
        $this->assign('action', 'topic');
        $this->display();
    }

    
    public function del(){
        $Id = I('post.data', 0, 'intval');
        $where['topic_id'] = $Id;

        $re = M('topic')->where($where)->delete();
        if ($re) {
            $this->success('删除活动专题成功！');
        } else {
            $this->error('删除活动专题失败！');
        }
    }
    
    
    
    
}
